package cn.liziguo.tank.factory;

import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.constants.ImageConst;
import cn.liziguo.tank.script.ShockWaveComponent;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import javafx.geometry.Point2D;
import javafx.scene.image.Image;
import javafx.util.Duration;

/**
 * @author Liziguo
 * @date 2022-07-09 20:32:16
 */
public final class EffectsFactory implements EntityFactory {

    public static void createBoom(Point2D point) {
//        FXGL.play(AudioConst.BOOM);
        Duration seconds = Duration.seconds(0.5);
        AnimationChannel ac = new AnimationChannel(FXGL.image(ImageConst.BOOM), seconds, 9);
        AnimatedTexture at = new AnimatedTexture(ac);
        Entity entity = FXGL.entityBuilder()
                .type(GameEnum.BOOM)
                .at(point.subtract(at.getFitWidth() / 2, at.getFitHeight() / 2))
                .view(at.play())
                .with(new ExpireCleanComponent(seconds))
                .build();

        FXGL.getGameWorld().addEntity(entity);
    }

    public static Entity createBigBoom(Point2D point, double radius, boolean camp, int attack) {
        //图片半径 = 有效的爆炸半径 / 0.65
        final double blastRange = 0.6;
//        FXGL.play(AudioConst.BOOM);
        Duration seconds = Duration.seconds(0.8);
        Image image = FXGL.image(ImageConst.BOOM_1);
        AnimationChannel ac = new AnimationChannel(image, 8, (int) (image.getWidth() / 8), (int) (image.getHeight() / 6), seconds, 0, 48);
        AnimatedTexture at = new AnimatedTexture(ac);

        final double magnification = radius / blastRange / (at.getFitWidth() / 2);
        Entity entity = FXGL.entityBuilder()
                .type(GameEnum.BOOM)
                .view(at.play())
                .at(point.subtract(at.getFitWidth() * magnification / 2, at.getFitHeight() * magnification / 2))
                .scale(magnification, magnification)
                .with(new ShockWaveComponent(point, radius, 0, (int) seconds.divide(2).toMillis(), camp, attack))
                .with(new ExpireCleanComponent(seconds))
                .build();

        FXGL.getGameWorld().addEntity(entity);
        return entity;
    }
}
